import 'package:flutter/cupertino.dart';
import 'package:flutter_smart_dialog/flutter_smart_dialog.dart';
import 'package:get/get.dart';

import '../../isar/isar_key_value_model.dart';

class GetLoginLogic extends GetxController {

  // 控制器和节点
  final FocusNode focusNode = FocusNode();
  late TextEditingController accountTextEditController;
  late TextEditingController passwordTextEditController;

  // 状态管理
  RxBool pwdEntering = false.obs;
  RxBool showPassword = false.obs;

  @override
  void onInit() async {
    super.onInit();
    focusNode.addListener(_focusChange);
    _initControllers();
  }


  void _initControllers() {
    accountTextEditController = TextEditingController(text:"admin");
    passwordTextEditController = TextEditingController(text: "123456");


  }

  ///跳转到跨页面
  void login() {

    if(accountTextEditController.text== 'admin' && passwordTextEditController.text == '123456'){
      _saveUserAccount();
      SmartDialog.showLoading();
      Future.delayed(const Duration(seconds: 3), () async {
        SmartDialog.dismiss();
        Get.toNamed('/main');
      });
      return;
    }else{
      SmartDialog.showToast('账号密码错误');
    }

    // Get.toNamed('home');
  }

  void _saveUserAccount() {
    IsarKeyValueModel.save(IsarKeyValueModel.create(
        IsarKeyValueModel.account,accountTextEditController.text));
    IsarKeyValueModel.save(IsarKeyValueModel.create(
        IsarKeyValueModel.password, passwordTextEditController.text));
  }

  void switchVisibility() {
    showPassword.value = !showPassword.value;
  }

  void _focusChange() {
    pwdEntering.value = focusNode.hasFocus;
  }


}
